<html>
<head><meta charset="utf-8"><title>as cast on high-rank fn type · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html">as cast on high-rank fn type</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="179701758"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179701758" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179701758">(Nov 02 2019 at 13:08)</a>:</h4>
<p>Is there a way to cast a <code>for&lt;'a&gt; fn(&amp;'a i32)</code> into a <code>fn(*const i32)</code> ?</p>



<a name="179701849"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179701849" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179701849">(Nov 02 2019 at 13:11)</a>:</h4>
<p><code>transmute</code></p>



<a name="179701997"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179701997" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179701997">(Nov 02 2019 at 13:15)</a>:</h4>
<p>It's definitely dubious whether the resulting function pointer can be called without UB.</p>



<a name="179703189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703189">(Nov 02 2019 at 13:49)</a>:</h4>
<p>(deleted)</p>



<a name="179703249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703249">(Nov 02 2019 at 13:50)</a>:</h4>
<p><span class="user-mention" data-user-id="116118">@Matthew Jasper</span> can you show an example? That cannot work if the <code>for&lt;'a&gt; fn(&amp;'a i32)</code> is a zero-sized type and the <code>fn(*const i32)</code> is not</p>



<a name="179703409"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703409" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703409">(Nov 02 2019 at 13:55)</a>:</h4>
<p><code>fn(&amp;'a i32)</code> is always a fn-ptr and not zero-sized, right?</p>



<a name="179703415"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703415" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703415">(Nov 02 2019 at 13:55)</a>:</h4>
<p>it seems like that should always be a valid cast, so long as you call the latter with valid ptrs (i.e., essentially references)</p>



<a name="179703422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703422">(Nov 02 2019 at 13:55)</a>:</h4>
<p>that type yes, but a function with that type not necessarily</p>



<a name="179703463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703463">(Nov 02 2019 at 13:56)</a>:</h4>
<p>I would expect that you can cast the function to that function ptr type</p>



<a name="179703470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703470">(Nov 02 2019 at 13:56)</a>:</h4>
<p>I fixed this by doing:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">foo</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="p">(</span><span class="n">_</span>: <span class="kp">&amp;</span><span class="na">&#39;a</span><span class="w"> </span><span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">x</span>: <span class="nc">for</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="w"> </span><span class="k">fn</span><span class="p">(</span><span class="o">&amp;</span><span class="na">&#39;a</span><span class="w"> </span><span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">foo</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">y</span>: <span class="nc">fn</span><span class="p">(</span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">transmute</span><span class="p">(</span><span class="n">x</span><span class="p">);</span><span class="w"></span>
</pre></div>



<a name="179703474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703474">(Nov 02 2019 at 13:56)</a>:</h4>
<p>If you have a function item then use a closure:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">f</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="p">(</span><span class="n">v</span>: <span class="kp">&amp;</span><span class="na">&#39;a</span><span class="w"> </span><span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">g</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">fn</span><span class="p">(</span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">|</span><span class="n">x</span><span class="o">|</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">f</span><span class="p">(</span><span class="o">&amp;*</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="179703475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703475">(Nov 02 2019 at 13:56)</a>:</h4>
<p>I did not knew that one can already use <code>for&lt;'a&gt;</code> in patterns</p>



<a name="179703493"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703493" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703493">(Nov 02 2019 at 13:57)</a>:</h4>
<p>I did not have a function item, I had a <code>F: Fn</code></p>



<a name="179703498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703498">(Nov 02 2019 at 13:57)</a>:</h4>
<p><a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2015&amp;gist=a8c261019d44a11af4969e46b390a10d" target="_blank" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2015&amp;gist=a8c261019d44a11af4969e46b390a10d">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2015&amp;gist=a8c261019d44a11af4969e46b390a10d</a><br>
^^^ is what I ended up doing</p>



<a name="179703545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703545">(Nov 02 2019 at 13:58)</a>:</h4>
<p>miri says that's fine</p>



<a name="179703546"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703546" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703546">(Nov 02 2019 at 13:58)</a>:</h4>
<p>That link's broken</p>



<a name="179703623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703623">(Nov 02 2019 at 14:00)</a>:</h4>
<p>fixed</p>



<a name="179703699"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703699" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703699">(Nov 02 2019 at 14:02)</a>:</h4>
<p>You could also have done something like this:</p>
<div class="codehilite"><pre><span></span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">foo</span><span class="o">&lt;</span><span class="n">F</span>: <span class="nb">Fn</span><span class="p">()</span><span class="o">&gt;</span><span class="p">(</span><span class="n">callback</span>: <span class="nc">F</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">c_fn</span><span class="p">(</span><span class="w"></span>
<span class="w">        </span><span class="o">|</span><span class="n">ptr</span><span class="p">,</span><span class="w"> </span><span class="p">()</span><span class="o">|</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">&lt;</span><span class="n">F</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nb">Fn</span><span class="o">&lt;</span><span class="p">()</span><span class="o">&gt;&gt;</span>::<span class="n">call</span><span class="p">(</span><span class="o">&amp;*</span><span class="p">(</span><span class="n">ptr</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">F</span><span class="p">),</span><span class="w"> </span><span class="p">())</span><span class="w"> </span><span class="p">},</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;</span><span class="n">callback</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">c_void</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="179703714"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703714" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703714">(Nov 02 2019 at 14:03)</a>:</h4>
<p>you, you don't need anything like fn-traits for that</p>



<a name="179703716"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703716" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703716">(Nov 02 2019 at 14:03)</a>:</h4>
<p>you can do this on stable, I think</p>



<a name="179703727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703727">(Nov 02 2019 at 14:03)</a>:</h4>
<p>what is the point of the unit tuple second argument?</p>



<a name="179703786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703786">(Nov 02 2019 at 14:05)</a>:</h4>
<p><a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2015&amp;gist=dbc943c2702a8fc3c8765b15cf238851" target="_blank" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2015&amp;gist=dbc943c2702a8fc3c8765b15cf238851">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2015&amp;gist=dbc943c2702a8fc3c8765b15cf238851</a></p>



<a name="179703790"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179703790" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179703790">(Nov 02 2019 at 14:05)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">foo</span><span class="o">&lt;</span><span class="n">F</span>: <span class="nb">Fn</span><span class="p">()</span><span class="o">&gt;</span><span class="p">(</span><span class="n">callback</span>: <span class="nc">F</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">c_fn</span><span class="p">(</span><span class="w"></span>
<span class="w">            </span><span class="o">|</span><span class="n">data</span><span class="p">,</span><span class="w"> </span><span class="p">()</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="p">(</span><span class="o">*</span><span class="p">(</span><span class="n">data</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">F</span><span class="p">))()</span><span class="w"></span>
<span class="w">            </span><span class="p">},</span><span class="w"></span>
<span class="w">            </span><span class="o">&amp;</span><span class="n">callback</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">c_void</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="179704132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179704132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179704132">(Nov 02 2019 at 14:15)</a>:</h4>
<blockquote>
<p>what is the point of the unit tuple second argument?</p>
</blockquote>
<p>I got a type error if I did not use it</p>



<a name="179704422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179704422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179704422">(Nov 02 2019 at 14:24)</a>:</h4>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> <span class="user-mention" data-user-id="116118">@Matthew Jasper</span> so what's going on there ?</p>



<a name="179704430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179704430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179704430">(Nov 02 2019 at 14:25)</a>:</h4>
<p>The closure is being safely implicitly coerced</p>



<a name="179704444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179704444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179704444">(Nov 02 2019 at 14:25)</a>:</h4>
<p>but there is no way to spell out the coercions ?</p>



<a name="179704452"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179704452" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179704452">(Nov 02 2019 at 14:25)</a>:</h4>
<p>Uh, you can just cast the closure to the fn ptr type</p>



<a name="179704494"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179704494" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179704494">(Nov 02 2019 at 14:26)</a>:</h4>
<p>But there's really no point</p>



<a name="179704532"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179704532" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179704532">(Nov 02 2019 at 14:27)</a>:</h4>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> can I cast the callback to the fn ptr type ?</p>



<a name="179704571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179704571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179704571">(Nov 02 2019 at 14:28)</a>:</h4>
<p>I guess that's how I approached the problem</p>



<a name="179704587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179704587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179704587">(Nov 02 2019 at 14:29)</a>:</h4>
<p>(i.e. I wanted to pass <code>callback</code> to <code>c_fn</code> in <code>foo</code>, but instead of creating a closure, i tried to do that directly)</p>



<a name="179714312"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179714312" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179714312">(Nov 02 2019 at 19:01)</a>:</h4>
<p>should be able to, yeah</p>



<a name="179746772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179746772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179746772">(Nov 03 2019 at 13:11)</a>:</h4>
<blockquote>
<p><span class="user-mention silent" data-user-id="116118">Matthew Jasper</span> can you show an example? That cannot work if the <code>for&lt;'a&gt; fn(&amp;'a i32)</code> is a zero-sized type and the <code>fn(*const i32)</code> is not</p>
</blockquote>
<p>that fn type is never a ZST, I think you are getting confused by the zero-sized anonymous singleton types we have for each function?</p>



<a name="179746816"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179746816" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179746816">(Nov 03 2019 at 13:12)</a>:</h4>
<p>these are <em>not</em> <code>fn</code> types</p>



<a name="179746830"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179746830" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179746830">(Nov 03 2019 at 13:13)</a>:</h4>
<p><a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/enum.TyKind.html#variant.FnDef" target="_blank" title="https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/enum.TyKind.html#variant.FnDef">https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/enum.TyKind.html#variant.FnDef</a> is a ZST that doesnt have surface syntax, it is the type of <code>foo</code> when <code>foo</code> is the name of a fn item;<br>
<a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/enum.TyKind.html#variant.FnPtr" target="_blank" title="https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/enum.TyKind.html#variant.FnPtr">https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/enum.TyKind.html#variant.FnPtr</a> is the thing that the <code>fn</code> type syntax compiles to</p>



<a name="179746876"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179746876" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179746876">(Nov 03 2019 at 13:14)</a>:</h4>
<p><code>FnDef</code> coerces to <code>FnPtr</code>, but that is an effectful coercion, not just a transmute (similar to e.g. coercing <code>&amp;[T; N]</code> to <code>&amp;[T]</code>)</p>



<a name="179818248"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179818248" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179818248">(Nov 04 2019 at 09:43)</a>:</h4>
<blockquote>
<p>should be able to, yeah</p>
</blockquote>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> can you show how ?</p>



<a name="179818299"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179818299" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179818299">(Nov 04 2019 at 09:44)</a>:</h4>
<p>My original link was an attempt at that, and required two unstable features.</p>



<a name="179830419"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179830419" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179830419">(Nov 04 2019 at 12:56)</a>:</h4>
<p>oh, I remembered wrong -- you can't cast closures to extern C, you need a shim. This, though, should work: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2015&amp;gist=902329e977c8e1467f7b15fd10549cec" target="_blank" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2015&amp;gist=902329e977c8e1467f7b15fd10549cec">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2015&amp;gist=902329e977c8e1467f7b15fd10549cec</a> -- the insight is that you can have a extern "C" function that's generic; it's not something that's in your public interface (mangling is still a thing, since it's generic), but you can pass it's address just fine</p>



<a name="179830422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179830422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179830422">(Nov 04 2019 at 12:56)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> ^</p>



<a name="179830433"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179830433" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179830433">(Nov 04 2019 at 12:56)</a>:</h4>
<p>See <a href="https://github.com/rust-lang/rust/issues/44291" target="_blank" title="https://github.com/rust-lang/rust/issues/44291">https://github.com/rust-lang/rust/issues/44291</a> for more</p>



<a name="179830578"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179830578" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179830578">(Nov 04 2019 at 12:58)</a>:</h4>
<p>Thanks, I guess what I was missing from my original approach is the <code>*(data as *const F)()</code> part</p>



<a name="179830593"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179830593" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179830593">(Nov 04 2019 at 12:58)</a>:</h4>
<p>I was trying to cast the <code>F</code> directly somehow</p>



<a name="179831184"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179831184" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179831184">(Nov 04 2019 at 13:08)</a>:</h4>
<p>right, yeah, you can't do that</p>



<a name="179831187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179831187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179831187">(Nov 04 2019 at 13:08)</a>:</h4>
<p>since it has "metadata"</p>



<a name="179831193"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/as%20cast%20on%20high-rank%20fn%20type/near/179831193" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/as.20cast.20on.20high-rank.20fn.20type.html#179831193">(Nov 04 2019 at 13:08)</a>:</h4>
<p>well, associated data, not really meta</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>